var Sequelize = require('sequelize');
var sequelize = new Sequelize('nodejs', 'root', '');

/*
var User = sequelize.define('UserTest', {
    username: Sequelize.STRING,
    birthday: Sequelize.DATE
});

sequelize.sync().then(function() {
    return User.create({
        username: 'janedoe',
        birthday: new Date(1980, 6, 20)
    });
}).then(function(jane) {
    console.log(jane.get({
        plain: true
    }));
});
*/
//http://sequelize.readthedocs.org/en/latest/docs/getting-started/

var User = sequelize.define('user',{
    id:{type:Sequelize.INTEGER,
        primaryKey:true,
        autoIncrement:true
    }
    ,
    name:Sequelize.STRING,
    departmentId:Sequelize.INTEGER
},{
    timestamps:false,
    freezeTableName:true
});
//插入记录
//User.create({name:'woshidongge',departmentId:null});//success

//upsert可以用于编辑以及添加记录，如果id不存在就会新建记录
//INSERT INTO `user` (`id`,`name`,`departmentId`) VALUES
// (18,'upsert is called',NULL) ON DUPLICATE KEY UPDATE `id`=VALUES(`id`),
// `name`=VALUES(`name`), `departmentId`=VALUES(`departmentId`);
//User.upsert({id:18,name:'upsert is called',departmentId:null});

//User.findAll({
//    where:{
//        name:'zeus'
//    }
//}).then(function(rows){
//    rows.forEach(function(row){
//        console.log(row.dataValues)
//    })
//})
//$gt用于查找id大于10的记录
//User.findAll({
//    where:{
//        id:{$gt:10}
//    }
//}).then(function(rows){
//    rows.forEach(function(row){
//        console.log(row.dataValues)
//    })
//})
//offset和limit用于分页
//User.findAll({
//    where:{
//        id:{$gt:10}
//    },
//    offset:2,
//    limit:2
//}).then(function(rows){
//    rows.forEach(function(row){
//        console.log(row.dataValues)
//    })
//})
var Department = sequelize.define('department',{
    id:{type:Sequelize.INTEGER,
        primaryKey:true,
        autoIncrement:true
    }
    ,
    name:Sequelize.STRING,
    parentId:Sequelize.INTEGER
},{
    timestamps:false,
    freezeTableName:true
});

var Right = sequelize.define('right',{
    id:{type:Sequelize.INTEGER,
        primaryKey:true,
        autoIncrement:true
    }
    ,
    name:Sequelize.STRING,
    url:Sequelize.STRING,
    parentId:Sequelize.INTEGER
},{
    timestamps:false,
    freezeTableName:true
});

var Role = sequelize.define('role',{
    id:{type:Sequelize.INTEGER,
        primaryKey:true,
        autoIncrement:true
    },
    name:Sequelize.STRING,
},{
    timestamps:false,
    freezeTableName:true
});

/*
sequelize.sync().then(function(){
    return User.create({
        name:'xxoo',
        departmentId:null
    });
}).then(function(x){
    console.log(x);
})*/
//User.findAll({
//    where:{id:18}
//}).then(function(data){
//    console.log(data[0].dataValues);
//})
//
//Department.findAll().then(function(data){
//    data.forEach(function(row){
//        console.log(row.dataValues);
//    })
//})